package cn.sinata.wanglin.neighbor.fragment;

import android.support.v7.widget.LinearLayoutManager;
import android.text.TextUtils;
import android.view.View;

import com.xilada.xldutils.adapter.BaseRecyclerAdapter;
import com.xilada.xldutils.fragment.BaseLazyFragment;
import com.xilada.xldutils.utils.ActivityUtil;
import com.xilada.xldutils.utils.SharedPreferencesUtils;
import com.xilada.xldutils.view.SwipeRefreshRecyclerLayout;

import java.util.ArrayList;

import butterknife.BindView;
import cn.sinata.wanglin.R;
import cn.sinata.wanglin.adapter.NewFriendsAdapter;
import cn.sinata.wanglin.adapter.SearchUserAdapter;
import cn.sinata.wanglin.commonutils.Constant;
import cn.sinata.wanglin.model.NewFriend;
import cn.sinata.wanglin.model.SearchUser;
import cn.sinata.wanglin.model.Userinfo;
import cn.sinata.wanglin.neighbor.view.SearchEditText;
import cn.sinata.wanglin.net.HttpManager;
import cn.sinata.wanglin.net.model.util.ResultDataSubscriber;
import cn.sinata.wanglin.village.activity.AllUserInfoActivity;


/**
 *
 * Created by liaoxiang on 16/12/13.
 */

public class FindUserByIdFragment extends BaseLazyFragment{

    @BindView(R.id.mSwipeRefreshLayout)
    SwipeRefreshRecyclerLayout mSwipeRefreshLayout;
    @BindView(R.id.tv_search)
    SearchEditText tv_search;
    private SearchUserAdapter newFriendsAdapter;
    private ArrayList<SearchUser> mFriends = new ArrayList<>();
    private String userId;
    private String userAccount;

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.fragment_find_user_by_village;
    }

    @Override
    protected void onFirstVisibleToUser() {

        tv_search.setOnSearchActionListener(new SearchEditText.OnSearchActionListener() {
            @Override
            public void onSearch() {
                userAccount = tv_search.getText().toString().trim();
                if (TextUtils.isEmpty(userAccount)) {
                    showToast("请输入正确的开心网邻ID/手机号");
                    return;
                }
                searchUser();
            }
        });
        userId = SharedPreferencesUtils.getString(Constant.SP_USERID);
        mSwipeRefreshLayout.setLayoutManager(new LinearLayoutManager(context));
        mSwipeRefreshLayout.setMode(SwipeRefreshRecyclerLayout.Mode.Top);
        newFriendsAdapter = new SearchUserAdapter(mFriends);
        newFriendsAdapter.setOnItemClickListener(new BaseRecyclerAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                ActivityUtil.create(FindUserByIdFragment.this)
                        .go(AllUserInfoActivity.class)
                        .put("friendID",mFriends.get(position).getId())
                        .startForResult(16);
            }
        });
        mSwipeRefreshLayout.setAdapter(newFriendsAdapter);
        mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshRecyclerLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                searchUser();
            }

            @Override
            public void onLoadMore() {

            }
        });

        tv_search.setHint("开心网邻ID/手机号");
    }

    private void searchUser(){
        userAccount = tv_search.getText().toString().trim();
        if (TextUtils.isEmpty(userAccount)) {
            showToast("请输入正确的开心网邻ID/手机号");
            return;
        }
        HttpManager.searchUser(userId, userAccount)
                .subscribe(new ResultDataSubscriber<ArrayList<SearchUser>>(this) {
                    @Override
                    public void onSuccess(String msg, ArrayList<SearchUser> friends) {
                        mSwipeRefreshLayout.setRefreshing(false);
                        mFriends.clear();
                        if (friends.size() == 0) {
                            mSwipeRefreshLayout.setLoadMoreText("没有找到该用户");
                            newFriendsAdapter.notifyDataSetChanged();
                            return;
                        }
                        mFriends.addAll(friends);
                        newFriendsAdapter.notifyDataSetChanged();
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        mSwipeRefreshLayout.setRefreshing(false);
                    }
                });
    }

    @Override
    protected void onVisibleToUser() {

    }

    @Override
    protected void onInvisibleToUser() {

    }
}
